package au.com.handsunited.web.controller.project;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.service.ProjectService;
import au.com.handsunited.web.command.project.InviteProjectCommand;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class InviteProjectController extends SimpleFormController {

	private ProjectService projectService;


	private static Logger logger = LoggerFactory.getLogger(InviteProjectController.class);

	/**
	 *
	 */
	public InviteProjectController() {
		setCommandClass(InviteProjectCommand.class);
	}



	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {

		InviteProjectCommand inviteProjectCommand = (InviteProjectCommand) command;

		String projectName 	= inviteProjectCommand.getProjectName();
		String contactName 	= inviteProjectCommand.getContactPerson();
		String emailAddress = inviteProjectCommand.getEmailAddress();
		String message 		= inviteProjectCommand.getMessage();

		projectService.inviteProject(SessionSupport.getUser(request), projectName, contactName, emailAddress, message);

		Map<String, String> model = new HashMap<String, String>();
		model.put("projectName", projectName);
		ModelAndView mav = new ModelAndView(getSuccessView(), model);

		return mav;
	}


	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

}

